package com.elitel.frame.base.component;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
//import com.elitel.common.redis.RedisDbIndex;
//import com.elitel.common.redis.service.RedisCacheService;
import com.elitel.frame.base.service.GroupService;
import com.elitel.frame.main.dao.ext.ExtGroupMapper;
import com.elitel.frame.main.entity.ext.CfgGroupExt;
import com.elitel.frame.main.entity.vo.ZTreeDataModelVo;

@Component("redisCacheGroupServer")
public class RedisCacheGroupServer {

//    @Autowired
//    private RedisCacheService redisCacheService;
    @Autowired
    private GroupService groupService;
//    @Autowired
//    private RedisConnInfo redisConnInfo;

    /**
     * <pre>getGroupListForTree(获取分组树)
     * @author：FanWenquan
     * @date：2017年11月1日 下午3:57:16
     * @param fatureid
     * @return</pre>
     * <p>
     * modify by guoyanfei on 2019/07/16
     * 新增验证redis连通性
     *
     * @see
     */
    @Cacheable(cacheNames = "DEFAULT", key = "'TREE_groupdata'")
    public List<ZTreeDataModelVo> getGroupListForTree(int fatureid) {
//		int xx = RedisDbIndex.value;
//		List<ZTreeDataModelVo> zTreeDataModelVos = new ArrayList<ZTreeDataModelVo>();
//		if(redisConnInfo.checkConnReids()){
//			String key = "TREE_groupdata";
//			if(redisCacheService.stringExists(key,xx)){
//				String ztreestr = redisCacheService.getObject(key, xx);
//				zTreeDataModelVos = (List<ZTreeDataModelVo>)JSON.parseArray(ztreestr,ZTreeDataModelVo.class);
//			}else{
//				List<ZTreeDataModelVo> parentList = groupService.getGroupListForTree(fatureid);
//				zTreeDataModelVos = parentList;
//				String ztreestr = JSON.toJSONString(parentList);
//				redisCacheService.saveObject(key, ztreestr, xx);
//			}
//		}else {
//			List<ZTreeDataModelVo> parentList = groupService.getGroupListForTree(fatureid);
//			zTreeDataModelVos = parentList;
//		}

        List<ZTreeDataModelVo> zTreeDataModelVos = groupService.getGroupListForTree(fatureid);
        return zTreeDataModelVos;
    }

    /**
     * <pre>freshGroupListForTree(刷新分组树缓存)
     * @author：FanWenquan
     * @date：2017年11月1日 下午4:04:56   </pre>
     * <p>
     * modify by guoyanfei on 2019/07/16
     * 新增验证redis连通性
     *
     * @see
     */
    @CachePut(cacheNames = CacheConfig.DEFAULT,key = "'TREE_groupdata'")
    public List<ZTreeDataModelVo> freshGroupListForTree() {
//        if (!redisConnInfo.checkConnReids()) return;
//
//        int xx = RedisDbIndex.value;
//        String key = "TREE_groupdata";
//        List<ZTreeDataModelVo> parentList = groupService.getGroupListForTree(0);
//        String ztreestr = JSON.toJSONString(parentList);
//        redisCacheService.saveObject(key, ztreestr, xx);
        List<ZTreeDataModelVo> parentList = groupService.getGroupListForTree(0);
        return parentList;
    }
}
